package com.xiyin.controller;


import com.xiyin.entity.UserSqlInjector;
import com.xiyin.service.IUserSqlInjectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能点: 1.SQL注入器（运行时动态地构建SQL语句）
 *        2.@SelectProvider 是 MyBatis 提供的一个注解，用于动态生成 SQL 语句
 * @author xukaihang
 * @since 2024-08-18
 */
@RestController
@RequestMapping("/user-sql-injector")
public class UserSqlInjectorController {
    @Autowired
    private IUserSqlInjectorService userSqlInjectorService;

    @GetMapping("/users")
    public List<UserSqlInjector> getUsers() {
        return userSqlInjectorService.zidingyisql();
    }

    //查询
    @GetMapping("{id}")
    public ResponseEntity<UserSqlInjector> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(userSqlInjectorService.getBaseMapper().selectById(id));
        //return ResponseEntity.ok(userSqlInjectorService.getById(id));
    }

    /**
     * @SelectProvider 是 MyBatis 提供的一个注解，用于动态生成 SQL 语句
     * @Date 2024/8/18 17:10
     **/
    @GetMapping
    public List<UserSqlInjector> findAll(
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "age", required = false) Integer age
    ) {
        UserSqlInjector user = new UserSqlInjector();
        user.setUserName(userName);
        user.setAge(age);
        return userSqlInjectorService.dynamicSelect(user);
    }
}
